﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GestionTaxesRiaApp.Web;

namespace GestionTaxesRiaApp.Views
{
    public partial class AjouterLogement : Page
    {
        GestionTaxesContext gestionTaxesContext = new GestionTaxesContext();

        public AjouterLogement()
        {
            SetDroits();
            InitializeComponent();
            var proprietaires = gestionTaxesContext.Load<Utilisateur>(gestionTaxesContext.GetUtilisateurProprietaireQuery(), false);
        }

        // S'exécute lorsque l'utilisateur navigue vers cette page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Valider_Click(object sender, RoutedEventArgs e)
        {
            Logement logement = new Logement();
            if (LogementTb.Text == null || LogementTb.Text.Equals(""))
            {
                MessageBox.Show("Veuillez remplir le champ logement");
            }
            else
            {
                logement.log_Nom = LogementTb.Text;
                logement.log_IdTarif = Int32.Parse(tar_IdTextBox.Text);
                logement.log_IdProprietaire = Int32.Parse(uti_IdTextBox.Text);
                logement.log_Type = CategorieCb.SelectionBoxItem.ToString();
                gestionTaxesContext.Logements.Add(logement);
                gestionTaxesContext.SubmitChanges();
                MessageBox.Show("Le logement a été créé");
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/GestionLogement", UriKind.Relative));
            }

        }

        private void RetourBt_Click(object sender, RoutedEventArgs e)
        {

        }

        private void logementDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void proprietaireDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void tarifDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void utilisateurDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void fonctionDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void reductionDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }
        private void SetDroits()
        {
            if (Connexion.utilisateurStatic.uti_IdFonction != Home.administrateurInt && Connexion.utilisateurStatic.uti_IdFonction != Home.gestionnaireInt)
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/Gestions", UriKind.Relative));
            }
            if (Connexion.utilisateurStatic.uti_IdFonction == Home.decoInt)
            {
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("/GestionTaxesRiaAppTestPage.aspx#/Connexion", UriKind.Relative));
            }
        }

    }
}
